package com.poly.police.monitor.admin.controller.sys;

import com.poly.police.monitor.application.service.sys.ISysLoginService;
import com.poly.police.monitor.common.aop.NotLogin;
import com.poly.police.monitor.common.aop.NotPower;
import com.poly.police.monitor.common.core.AjaxResult;
import com.poly.police.monitor.pojo.validate.sys.SysAdminLoginsValidate;
import com.poly.police.monitor.pojo.vo.sys.SysCaptchaVo;
import com.poly.police.monitor.pojo.vo.sys.SysLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 系统登录管理
 */
@RestController
@RequestMapping("api/sys")
@Api(tags = "系统登录管理")
public class SysLoginController {

    @Resource
    ISysLoginService iSysLoginService;

    /**
     * 取验证码
     */
    @NotLogin
    @GetMapping("/captcha")
    @ApiOperation(value="取验证码")
    public AjaxResult<SysCaptchaVo> captcha() {
        SysCaptchaVo vo = iSysLoginService.captcha();
        return AjaxResult.success(vo);
    }

    /**
     * 登录系统
     * @param loginsValidate 请求数据
     * @return SysLoginVo 响应数据
     */
    @NotLogin
    @PostMapping("/login")
    @ApiOperation(value="登录系统")
    public AjaxResult<SysLoginVo> login(@Validated() @RequestBody SysAdminLoginsValidate loginsValidate) {
        SysLoginVo vo = iSysLoginService.login(loginsValidate);
        return AjaxResult.success(vo);
    }

    /**
     * 退出登录
     * @param request 请求数据
     */
    @NotPower
    @PostMapping("/logout")
    @ApiOperation(value="退出登录")
    public AjaxResult<?> logout(HttpServletRequest request) {
        iSysLoginService.logout(request.getHeader("token"));
        return AjaxResult.success();
    }

}
